// by Xeno
#define THIS_FILE "fn_vec_hudsp.sqf"
#include "x_setup.sqf"
private ["_speed_str", "_fuel_str", "_dam_str", "_dir_str", "_gendirlist", "_vec", "_welcome_str", "_vec_msg1", "_struct_text", "_endtime", "_type_name", "_vec_string", "_vdir", "_gendir", "_dstr", "_count", "_control", "_type_weap", "_dirtmp", "_fuelcap"];
_speed_str = (localize "STR_DOM_MISSIONSTRING_611");
_fuel_str =  (localize "STR_DOM_MISSIONSTRING_612");
_dam_str =   (localize "STR_DOM_MISSIONSTRING_613");
_dir_str =   (localize "STR_DOM_MISSIONSTRING_614");
_gendirlist = ["N",(localize "STR_DOM_MISSIONSTRING_615"),(localize "STR_DOM_MISSIONSTRING_616"),(localize "STR_DOM_MISSIONSTRING_617"),"E",(localize "STR_DOM_MISSIONSTRING_618"),(localize "STR_DOM_MISSIONSTRING_619"),(localize "STR_DOM_MISSIONSTRING_620"),"S",(localize "STR_DOM_MISSIONSTRING_621"),(localize "STR_DOM_MISSIONSTRING_622"),(localize "STR_DOM_MISSIONSTRING_623"),"W",(localize "STR_DOM_MISSIONSTRING_624"),(localize "STR_DOM_MISSIONSTRING_625"),(localize "STR_DOM_MISSIONSTRING_626"),"N"];

disableSerialization;

#define __CTRL(A) (_disp displayCtrl A)

_vec = vehicle player;
if (_vec isKindOf "LandVehicle" && {!(_vec isKindOf "StaticWeapon")}) then {
	if (GVAR(show_vehicle_welcome) == 0) then {
		_vtype = GV(_vec,GVAR(vec_type));
		if (!isNil "_vtype" && {_vtype == "MHQ" || {_vtype == "Engineer"}}) then {
			[_vec, _vtype] spawn FUNC(vec_welcome_message);
		};
	};
	_fuelcap = getNumber(configFile/"CfgVehicles"/typeOf _vec/"fuelCapacity");
	while {GVAR(player_in_vec) && {alive player} && {!__pGetVar(xr_pluncon)}} do {
		if (player == driver _vec || {player == gunner _vec} || {player == commander _vec}) then {
			_type_name = [typeOf _vec,0] call FUNC(GetDisplayName);
			_vec_string = format [(localize "STR_DOM_MISSIONSTRING_630"), _type_name];
			67327 cutRsc ["xvehicle_hud", "PLAIN"];
			_disp = __uiGetVar(DVEC_HUD);
			_ison = true;
			while {GVAR(player_in_vec) && {alive player} && {player == driver _vec}} do {
				_crewctrl = __uiGetVar(GVAR(rscCrewText)) displayCtrl 9999;
				_cctrlshown = if (isNil "_crewctrl") then {false} else {ctrlShown _crewctrl};
				if (!visibleMap && {!_cctrlshown}) then {
					if (!_ison) then {
						67327 cutRsc ["xvehicle_hud", "PLAIN"];
						_disp = __uiGetVar(DVEC_HUD);
						_ison = true;
					};
					
					__CTRL(64432) ctrlSetText _vec_string;
					__CTRL(64433) ctrlSetText format [_speed_str, round (speed _vec)];
					__CTRL(64434) ctrlSetText format [_fuel_str, round (fuel _vec * _fuelcap)];
					__CTRL(64435) ctrlSetText format [_dam_str, round (damage _vec * 100)];
					_vdir = round (direction _vec);
					_gendir = _gendirlist select (round (_vdir/22.5));
					__CTRL(64436) ctrlSetText format [_dir_str, _vdir, _gendir];
				} else {
					if (_ison) then {
						67327 cutRsc ["XDEmpty", "PLAIN"];
						_ison = false;
					};
				};
				sleep 0.331;
			};
			67327 cutRsc["XDEmpty", "PLAIN",1];
		};
		sleep 0.532;
	};
} else {
	if (_vec isKindOf "StaticWeapon" && {!(_vec isKindOf "StaticATWeapon")}) then {
		while {GVAR(player_in_vec) && {alive player} && {!__pGetVar(xr_pluncon)}} do {
			if (player == gunner _vec) then {
				_type_name = [typeOf _vec,0] call FUNC(GetDisplayName);
				_vec_string = (localize "STR_DOM_MISSIONSTRING_631") + _type_name;
				_type_weap = (getArray(configFile/"CfgVehicles"/(typeOf _vec)/"Turrets"/"MainTurret"/"weapons")) select 0;
				67327 cutRsc ["xvehicle_hud", "PLAIN"];
				_disp = __uiGetVar(DVEC_HUD);
				_ison = true;
				while {GVAR(player_in_vec) && {alive player} && {player == gunner _vec}} do {
					_crewctrl = __uiGetVar(GVAR(rscCrewText)) displayCtrl 9999;
					_cctrlshown = if (isNil "_crewctrl") then {false} else {ctrlShown _crewctrl};
					if (!visibleMap && {!_cctrlshown}) then {
						if (!_ison) then {
							67327 cutRsc ["xvehicle_hud", "PLAIN"];
							_disp = __uiGetVar(DVEC_HUD);
							_ison = true;
						};
						__CTRL(64432) ctrlSetText _vec_string;
						__CTRL(64433) ctrlSetText format [_dam_str, round (damage _vec * 100)];
						_vecwdir = _vec weaponDirection _type_weap;
						_dirtmp = round((_vecwdir select 0) atan2 (_vecwdir select 1));
						if (_dirtmp < 0) then {_dirtmp = _dirtmp + 360};
						_gendir = _gendirlist select (round (_dirtmp/22.5));
						__CTRL(64434) ctrlSetText format [_dir_str, _dirtmp, _gendir];
						__CTRL(64435) ctrlSetText "";
						__CTRL(64436) ctrlSetText "";
					} else {
						if (_ison) then {
							67327 cutRsc ["XDEmpty", "PLAIN"];
							_ison = false;
						};
					};
					sleep 0.331;
				};
				67327 cutRsc["XDEmpty", "PLAIN",1];
			};
			sleep 0.532;
		};
	};
};
